/**
 * @description AI的数据访问对象
 */

const { AITopic } = require('@models/ai-topic')
const { AIMessage } = require('@models/ai-message')

class AIDao {
    /**
     * topicList 获取话题列表
     * @param {*} params 
     * @returns 
     */
    static async topicList(params = {}) {
        // 默认分页 - 10篇文章为一页~
        const DEFAULT_PAGE_SIZE = 20
        const { category, page_size = DEFAULT_PAGE_SIZE, page = 1 } = params
        console.warn("params", params)

        // 转换数据
        const pageSize = Number(page_size);
        const pageNum = Number(page);

        // 筛选方式
        const filter = {}

        if (category) {
            filter.category = category;
        }

        // 查询数据
        const aiList = await AITopic.findAndCountAll({
            limit: pageSize, //每页20条
            offset: (pageNum - 1) * pageSize,
            where: filter,
        })

        const rows = aiList.rows
        try {
            const data = rows
            return [null, data]
        } catch (err) {
            return [err, null]
        }
    }

    /**
     * createMessage 创建消息
     */
    static async createMessage(v) {
        // 创建消息
        const message = new AIMessage()

        message.content = "Message hello"
        message.topic_id = 12

        try {
            const res = await message.save()
            return [null, res]
        } catch (err) {
            console.log(err)
            return [err, null]
        }
    }
}

module.exports = {
    AIDao
}
